package com.f.client;

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.f.javabean.PushMsg.Push;
import com.google.protobuf.ByteString;
import com.google.protobuf.ByteString.Output;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class FClientHandle extends ChannelInboundHandlerAdapter{

	private String filePath = "E:/filehandle/fileout/";
	private ConcurrentMap<String, ReadStream> readStreamMap = new ConcurrentHashMap<String, ReadStream>();
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelActive(ctx);
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelInactive(ctx);
	}
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		if(msg instanceof Push){
			Push push = (Push)msg;
			String filePath = "E:/filehandle/fileout/";
			String id = push.getId()+"";
			ReadStream rs = null;
			if(id.trim().equals("")){
				id = UUID.randomUUID().toString().replace("-", "");
				rs = new ReadStream(id, push, filePath+push.getFilename(), ctx.channel());
				readStreamMap.put(id, rs);
				rs.runs();
				return;
			}
			rs = readStreamMap.get(id);
			rs.setPush(push);
			rs.runs();
//			if(push.getIsstart()){
//				System.out.println("start...");
//				String fileName = push.getFilename();
//				filePath += fileName;
//				File file = new File(filePath);
//				if(file.exists()){
//					file.delete();
//				}
//				boolean cb = file.createNewFile();
//				if(cb){
//					System.out.println("create file success");
//					Push.Builder builder = Push.newBuilder();
//					builder.setId(push.getId());
//					builder.setType("canrun");
//					ctx.channel().writeAndFlush(builder);
//				}
//			}else if(push.getIsing()){
//				System.out.println("ing..."+push.getReadnum()+"%");
//				String fileName = push.getFilename();
//				filePath += fileName;
//				File file = new File(filePath);
//				FileOutputStream fos = null;
//				Output op = null;
//				try{
//					ByteString byteString = push.getFbyte();
//					byte[] bytes = new byte[1024];
//					byteString.copyTo(bytes, 0);
//					fos = new FileOutputStream(file,true);
//					fos.write(bytes);
//					fos.flush();
//				}catch(Exception e){
//					e.printStackTrace();
//				}finally{
//					if(fos != null ){
//						fos.close();
//						fos = null;
//					}
//					if(op != null){
//						op.close();
//						op = null;
//					}
//				}
//			}else if(push.getIsend()){
//				System.out.println("end");
//			}
		}else{
			super.channelRead(ctx, msg);
		}
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		// TODO Auto-generated method stub
		super.exceptionCaught(ctx, cause);
	}

}
